package com.app.controller;


import com.app.common.Result;
import com.app.model.UserModel;
import com.app.service.ISignInService;
import com.app.service.UserService;
import com.app.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;


/**
 * <p>
 * 签到表 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/signIn")
public class SignInController {
    @Autowired
    private ISignInService iSignInService;
    @Autowired
    private UserService userService;
    /**
     * 签到
     *
     * @return {@link Result}
     */
    @GetMapping("/signIn")
    public Result<Object> sign(HttpServletRequest request) throws ParseException {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        Object o = iSignInService.signIn(userId);
        return Result.success(o);
    }
}

